<template>
	<view class="content">
		<button @click="get_wx_user" class="bottom-btn" type="default" size="mini">登录</button>
		{{userInfo.cloudID}}
	</view>
</template>

<script>
	import {
		userLogin
	} from './api/index.js'
	export default {
		data() {
			return {
				userInfo: {
					cloudID: ''
				},
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {
			get_wx_user() {
				//请求后台,获取openId
				uni.request({
					url: `https://qmhly.hls.com/nine/appletLogin`,
					method: 'POST',
					data: {
						code: res.code,
						nickName: this.formData.nickName,
						avatarUrl: this.formData.avatarUrl
					},
					dataType: 'json',
					sslVerify: false,
					success: (res1) => {
						console.log("用户登录返回数据----", res1.data.data);
						this.openId = res1.data.data.openId;
						this.token = res1.data.data.user.access_token;
						let params = {
							sessionKey: res1.data.data.sessionKey,
							iv: this.iv,
							encryptedData: this.encryptedData,
							id: res1.data.data.openId,
						}

						//获取用户手机号
						appletPhone(params).then(res2 => {
							console.log("手机号返回---", res2);
							this.phoneNum = res2.data.phoneNumber;
							//登陆成功后，重新查询用户身份信息
							this.getUserInfo(this.openId);
						})

						//存储token至本地
						uni.setStorage({
							key: 'access_token',
							data: this.token,
							success: function() {
								console.log(
									'access_token存储成功'
								);
							},
							fail: function(error) {
								console.error(
									'access_token存储失败:',
									error);
							}
						});

						//将openId、nickName、avatarUrl存储到本地
						// 存储OpenID
						uni.setStorage({
							key: 'openId',
							data: res1.data.data.openId,
							success: function() {
								console.log(
									'OpenID存储成功'
								);
							},
							fail: function(error) {
								console.error(
									'OpenID存储失败:',
									error);
							}
						});
						uni.setStorage({
							key: 'nickName',
							data: this.formData.nickName,
							success: function() {
								console.log(
									'nickName存储成功'
								);
							},
							fail: function(error) {
								console.error(
									'nickName存储失败:',
									error);
							}
						});
						uni.setStorage({
							key: 'avatarUrl',
							data: this.formData.avatarUrl,
							success: function() {
								console.log(
									'avatarUrl存储成功'
								);
							},
							fail: function(error) {
								console.error(
									'avatarUrl存储失败:',
									error);
							}
						});
					}
				})
				// uni.getUserProfile({
				// 	desc: '获取您的头像、昵称及手机号',
				// 	success: res1 => {
				// 		this.userInfo = res1
				// 		console.log("aa", this.userInfo);
				// 	},
				// 	fail: err => {
				// 		console.log(err);
				// 	}
				// });
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>